package in.animeshpathak.androvita;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class TouchView extends View {

	    /** whether to draw blue circle or not*/
	    private static boolean ballInMotion = false;
	    /** location of ball */
	    private static float xLocation;
	    private static float yLocation;
	    
	    /** radius of ball */
	    private static float radius=20;
	    
	    private Paint mPaint;
	    
	    //radius scaling. For when it is static or moving.
	    private static final float RADIUS_SCALING_STATIC = 1;
	    private static final float RADIUS_SCALING_MOVING = 1.5f;
	    
	    private static float radiusScaling = RADIUS_SCALING_STATIC;
	    
	    

	    public TouchView(Context context) {
	        super(context);
	        initTouchView();

	    }
	    
	    public TouchView(Context c, AttributeSet a){
	    	super(c,a);
	    	initTouchView();
	    	return;
	    }

	    protected void onDraw(Canvas canvas) {
	        
	    	radius = radiusScaling*(getWidth()/10);
	    	Log.d("AP - TouchDemo","radius = " + radius);

	    	// using big and small balls right now, so no color coding for moving and static ball.
//	    	if(ballInMotion){
//	        	//moving ball
//		        mPaint.setColor(Color.BLUE);
//	        } else {
//	        	//static ball
//		        mPaint.setColor(Color.RED);
//	        }
        	canvas.drawCircle(xLocation, yLocation, radius, mPaint);
	        
	    }
	    
	    private void initTouchView(){
	    	
	    	xLocation = 100;
	    	yLocation = 100;
	    	Log.d("AP - TouchDemo","radius = " + radius);
	    	
	        mPaint = new Paint();
	        mPaint.setColor(Color.MAGENTA);
	    }
	    
	    /**
	     * To start drawing the ball
	     * @param x current x coordinate
	     * @param y current y coordinate
	     * @return true if I consumed the event, false if not.
	     */
	    public boolean touchDown(float x, float y){
	    	//if the touch was within the radius distance, only then start moving the ball. Otherwise nothing.
	    	if((Math.abs(x - xLocation) <= radius)
	    			&& (Math.abs(y - yLocation) <= radius)){
	    		ballInMotion = true;
	    		radiusScaling = RADIUS_SCALING_MOVING;
	    		xLocation = x;
	    		yLocation = y;
	    		invalidate();
	    		return true;
	    	}
	    	else 
	    		return false;
	    }
	    
	    /**
	     * called when ball is put back
	     * @param x new X coordinate
	     * @param y new Y coordinate
	     * @return true if I consumed the event, false if not.
	     */
	    public boolean touchUp(float x, float y){
	    	if(ballInMotion){
	    		ballInMotion = false;
	    		radiusScaling = RADIUS_SCALING_STATIC;
	    		invalidate();
	    		return true;
	    	}
	    	else
	    		return false;
	    }
	    
	    /**
	     * Move ball to a certain location
	     * @param x new X coordinate
	     * @param y new Y coordinate
	     * @return true if I consumed the event, false if not.
	     */
	    public boolean touchMove(float x, float y){
	    	if(ballInMotion){
	    		xLocation = x;
	    		yLocation = y;
	    		invalidate();
	    		return true;
	    	}
	    	else 
	    		return false;
	    }
}
